﻿using System;
using System.Collections.Generic;
using CommonFramework;
using CommonFramework.Log;
using Net.Client;

namespace Net.Server
{
    public interface INetEventHandler
    {
        void OnAccept(NetSession session);
        void OnConnectSucc(NetSession session);
        void OnConnectFailed(NetSession session, string reason, Exception e = null);
        void OnDisconnect(NetSession session);
        void OnReceivePacket(NetSession session, NetPacket packet);
    }

    public static class NetEventHandler
    {
        public static readonly HashSet<INetEventHandler> Listeners = new HashSet<INetEventHandler>();

        public static void OnAccept(NetSession session)
        {
            foreach (var listener in Listeners)
            {
                listener.OnAccept(session);
            }
        }

        public static void OnConnectSucc(NetSession session)
        {
            foreach (var listener in Listeners)
            {
                listener.OnConnectSucc(session);
            }
        }

        public static void OnConnectFaield(NetSession session, string reason, Exception e = null)
        {
            foreach (var listener in Listeners)
            {
                listener.OnConnectFailed(session, reason, e);
            }
        }

        public static void OnDisconnect(NetSession session)
        {
            foreach (var listener in Listeners)
            {
                listener.OnDisconnect(session);
            }
        }

        public static void OnReceivePacket(NetSession session, NetPacket packet)
        {
            foreach (var listener in Listeners)
            {
                listener.OnReceivePacket(session, packet);
            }
        }
    }
}